package com.sr.controller;

import com.sr.annotation.Log;
import com.sr.constant.Constant;
import com.sr.ibase.IBaseService;
import com.sr.pojo.SysLog;
import com.sr.service.ILogService;
import com.sr.util.R;
import com.sr.vo.PageVo;
import com.sr.vo.req.LogPageReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author xiaochi
 * @date 2022/5/3 20:35
 * @desc
 */
@Api(tags = "后台接口 -> 日志管理")
@RestController
@RequestMapping(Constant.Api.BACK_PREFIX)
@RequiredArgsConstructor(onConstructor = @__)
public class LogController extends BaseController{

    private final ILogService logService;

    @ApiOperation(value = "分页查找操作日志",notes = "分页查找操作日志接口")
    @PreAuthorize("hasAuthority('sys:log:list')")
    @PostMapping("/logs")
    public R<PageVo<SysLog>> pageInfo(@RequestBody LogPageReqVo reqVo){
        IBaseService.Criteria<SysLog> criteria = logService.createCriteria(SysLog.class);
        criteria.orderBy(SysLog::getCreateTime).desc();
        return R.ok(logService.findPage(reqVo.getPageNum(),reqVo.getPageSize(),criteria));
    }

    @ApiOperation(value = "删除日志",notes = "删除日志接口")
    @PreAuthorize("hasAuthority('sys:log:delete')")
    @Log(module = "系统管理-日志管理",action = "删除日志接口")
    @DeleteMapping("/log")
    public R<String> deletedLog(@RequestBody @ApiParam(value = "日志id集合") List<String> logIds){
        return this.response(logService.deletedLog(logIds));
    }

    @ApiOperation(value = "清空日志",notes = "清空日志接口")
    @PreAuthorize("hasAuthority('sys:log:clean')")
    @Log(module = "系统管理-日志管理",action = "清空日志接口")
    @DeleteMapping("/log/clean")
    public R<String> cleanLog(){
        return this.response(logService.cleanLog());
    }
}